/**
 * 音乐目录控制器
 *
 * 提供音乐目录相关的REST API接口，所有接口均需要JWT身份认证。
 * 这些接口允许用户创建、查看、更新和删除个人音乐目录，
 * 以及管理目录中的歌曲和播放列表。
 */
import {
  Controller,
  Post,
  Get,
  Put,
  Delete,
  Param,
  Body,
  Req,
  UseGuards,
} from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { MusicDirectoryService } from './music-directory.service';
import {
  CreateMusicDirectoryDto,
  UpdateMusicDirectoryDto,
  MusicDirectoryResponseDto,
  AddSongToDirectoryDto,
  AddPlaylistToDirectoryDto,
} from './music-directory.dto';
import { ApiResponse } from '../common/response.dto';
import { Song } from '../song/song.entity';
import { Playlist } from '../playlist/playlist.entity';

@Controller('music-directories')
@UseGuards(AuthGuard('jwt'))
export class MusicDirectoryController {
  constructor(private readonly musicDirectoryService: MusicDirectoryService) {}

  /**
   * 创建新的音乐目录
   *
   * @route POST /music-directories
   * @param req 请求对象，包含当前登录用户信息
   * @param createDto 创建目录的请求数据
   * @returns 创建的目录信息
   */
  @Post()
  async createDirectory(
    @Req() req,
    @Body() createDto: CreateMusicDirectoryDto,
  ): Promise<ApiResponse<MusicDirectoryResponseDto>> {
    const directory = await this.musicDirectoryService.createDirectory(
      req.user,
      createDto,
    );
    return new ApiResponse(201, '目录创建成功', directory, undefined, req.path);
  }

  /**
   * 获取用户的所有音乐目录
   *
   * @route GET /music-directories
   * @param req 请求对象，包含当前登录用户信息
   * @returns 用户所有目录的列表
   */
  @Get()
  async getUserDirectories(@Req() req) {
    return this.musicDirectoryService.getUserDirectories(req.user);
  }

  /**
   * 获取目录详情
   *
   * @route GET /music-directories/:id
   * @param req 请求对象，包含当前登录用户信息
   * @param directoryId 目录ID
   * @returns 目录详情，包含关联的歌曲和播放列表
   */
  @Get(':id')
  async getDirectoryById(
    @Req() req,
    @Param('id') directoryId: number,
  ): Promise<ApiResponse<MusicDirectoryResponseDto>> {
    const directory = await this.musicDirectoryService.getDirectoryById(
      req.user,
      directoryId,
    );
    return new ApiResponse(
      200,
      '获取目录详情成功',
      directory,
      undefined,
      req.path,
    );
  }

  /**
   * 更新目录信息
   *
   * @route PUT /music-directories/:id
   * @param req 请求对象，包含当前登录用户信息
   * @param directoryId 目录ID
   * @param updateDto 更新目录的请求数据
   * @returns 更新后的目录信息
   */
  @Put(':id')
  async updateDirectory(
    @Req() req,
    @Param('id') directoryId: number,
    @Body() updateDto: UpdateMusicDirectoryDto,
  ): Promise<ApiResponse<MusicDirectoryResponseDto>> {
    const directory = await this.musicDirectoryService.updateDirectory(
      req.user,
      directoryId,
      updateDto,
    );
    return new ApiResponse(200, '目录更新成功', directory, undefined, req.path);
  }

  /**
   * 删除目录
   *
   * @route DELETE /music-directories/:id
   * @param req 请求对象，包含当前登录用户信息
   * @param directoryId 目录ID
   * @returns 删除成功的响应
   */
  @Delete(':id')
  async deleteDirectory(
    @Req() req,
    @Param('id') directoryId: number,
  ): Promise<ApiResponse<void>> {
    await this.musicDirectoryService.deleteDirectory(req.user, directoryId);
    return new ApiResponse(200, '目录删除成功', undefined, undefined, req.path);
  }

  /**
   * 向目录添加歌曲
   *
   * @route POST /music-directories/:id/songs
   * @param req 请求对象，包含当前登录用户信息
   * @param directoryId 目录ID
   * @param addSongsDto 要添加的歌曲ID列表
   * @returns 更新后的目录信息
   */
  @Post(':id/songs')
  async addSongsToDirectory(
    @Req() req,
    @Param('id') directoryId: number,
    @Body() addSongsDto: AddSongToDirectoryDto,
  ): Promise<ApiResponse<MusicDirectoryResponseDto>> {
    const directory = await this.musicDirectoryService.addSongsToDirectory(
      req.user,
      directoryId,
      addSongsDto,
    );
    return new ApiResponse(200, '歌曲添加成功', directory, undefined, req.path);
  }

  /**
   * 从目录移除歌曲
   *
   * @route DELETE /music-directories/:id/songs/:songId
   * @param req 请求对象，包含当前登录用户信息
   * @param directoryId 目录ID
   * @param songId 要移除的歌曲ID
   * @returns 更新后的目录信息
   */
  @Delete(':id/songs/:songId')
  async removeSongFromDirectory(
    @Req() req,
    @Param('id') directoryId: number,
    @Param('songId') songId: number,
  ): Promise<ApiResponse<MusicDirectoryResponseDto>> {
    const directory = await this.musicDirectoryService.removeSongFromDirectory(
      req.user,
      directoryId,
      songId,
    );
    return new ApiResponse(200, '歌曲移除成功', directory, undefined, req.path);
  }

  /**
   * 获取目录中的所有歌曲
   *
   * @route GET /music-directories/:id/songs
   * @param req 请求对象，包含当前登录用户信息
   * @param directoryId 目录ID
   * @returns 目录中的歌曲列表
   */
  @Get(':id/songs')
  async getDirectorySongs(
    @Req() req,
    @Param('id') directoryId: number,
  ): Promise<ApiResponse<Song[]>> {
    const songs = await this.musicDirectoryService.getDirectorySongs(
      req.user,
      directoryId,
    );
    return new ApiResponse<Song[]>(
      200,
      '获取目录歌曲成功',
      songs,
      undefined,
      req.path,
    );
  }

  /**
   * 向目录添加播放列表
   *
   * @route POST /music-directories/:id/playlists
   * @param req 请求对象，包含当前登录用户信息
   * @param directoryId 目录ID
   * @param addPlaylistsDto 要添加的播放列表ID列表
   * @returns 更新后的目录信息
   */
  @Post(':id/playlists')
  async addPlaylistsToDirectory(
    @Req() req,
    @Param('id') directoryId: number,
    @Body() addPlaylistsDto: AddPlaylistToDirectoryDto,
  ): Promise<ApiResponse<MusicDirectoryResponseDto>> {
    const directory = await this.musicDirectoryService.addPlaylistsToDirectory(
      req.user,
      directoryId,
      addPlaylistsDto,
    );
    return new ApiResponse(
      200,
      '播放列表添加成功',
      directory,
      undefined,
      req.path,
    );
  }

  /**
   * 从目录移除播放列表
   *
   * @route DELETE /music-directories/:id/playlists/:playlistId
   * @param req 请求对象，包含当前登录用户信息
   * @param directoryId 目录ID
   * @param playlistId 要移除的播放列表ID
   * @returns 更新后的目录信息
   */
  @Delete(':id/playlists/:playlistId')
  async removePlaylistFromDirectory(
    @Req() req,
    @Param('id') directoryId: number,
    @Param('playlistId') playlistId: number,
  ): Promise<ApiResponse<MusicDirectoryResponseDto>> {
    const directory =
      await this.musicDirectoryService.removePlaylistFromDirectory(
        req.user,
        directoryId,
        playlistId,
      );
    return new ApiResponse(
      200,
      '播放列表移除成功',
      directory,
      undefined,
      req.path,
    );
  }

  /**
   * 获取目录中的所有播放列表
   *
   * @route GET /music-directories/:id/playlists
   * @param req 请求对象，包含当前登录用户信息
   * @param directoryId 目录ID
   * @returns 目录中的播放列表列表
   */
  @Get(':id/playlists')
  async getDirectoryPlaylists(
    @Req() req,
    @Param('id') directoryId: number,
  ): Promise<ApiResponse<Playlist[]>> {
    const playlists = await this.musicDirectoryService.getDirectoryPlaylists(
      req.user,
      directoryId,
    );
    return new ApiResponse<Playlist[]>(
      200,
      '获取目录播放列表成功',
      playlists,
      undefined,
      req.path,
    );
  }
}
